package cn.zhangjie.pet.controller;

import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.LoginContext;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.pet.domain.Pet;
import cn.zhangjie.pet.query.PetQuery;
import cn.zhangjie.pet.service.IPetService;
import cn.zhangjie.system.query.DetailQuery;
import cn.zhangjie.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class PetController {
    @Autowired
    private IPetService petService;




    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Pet> findall(HttpServletRequest request){
        try {
            LoginContext.getLogininfo(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return petService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条" )
    public Pet findone(@PathVariable("id") Long id){
        return petService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            petService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updataoradd(@RequestBody Pet pet){

        try {
            if(pet.getId()==null){
                petService.add(pet);
                return JsonResult.me();
            }else {
                petService.update(pet);
                return JsonResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Pet> pagequery(@RequestBody PetQuery petQuery){
        try {
            return petService.queryPage(petQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult pactchdel(@RequestBody List<Long> ids){

        try {
            petService.pactchdel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onsale(@RequestBody List<Long> ids){

        try {
            petService.onsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offsale(@RequestBody List<Long> ids){

        try {
            petService.offsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }


}
